package com.yixun.qfbao.hander;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yixun.common.page.Page;
import com.yixun.qfbao.apidto.PartnerCheckPageResDto;
import com.yixun.qfbao.apidto.PartnerCheckResDto;
import com.yixun.qfbao.dao.DepartmentDao;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.Department;
import com.yixun.qfbao.model.ModelInfo;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.model.dto.PartnerInfoDto;
import com.yixun.qfbao.model.enums.CheckStatusEnum;
import com.yixun.qfbao.model.enums.ModelTypeEnum2;
import com.yixun.qfbao.service.ModelInfoService;
import com.yixun.qfbao.service.PartnerInfoService;
import com.yixun.qfbao.util.StringUtil;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.vo.GetCheckListReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;

/**
 * @author zilong.liu
 * @date 2020-12-16
 *
 * desc:
 *     	根据总代companyUuid获取对应的经济公司规则
 */
@Service("getCheckListHander")
public class GetCheckListHander extends CommonAbstractHander{


	@Autowired
	PartnerInfoService partnerInfoService;
	
	@Autowired
    LoginUtils loginUtils;
	
	@Autowired
	ModelInfoService modelInfoService;
	
	@Autowired
	DepartmentDao departmentDao;
	
	@Override
	public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
		BaseResponseDTO<PartnerCheckPageResDto> res = new BaseResponseDTO<>();
		PartnerCheckPageResDto partnerCheckPageResDto = new PartnerCheckPageResDto();
		
		GetCheckListReqVo reqVo = (GetCheckListReqVo) reqDto;
		String userId = reqVo.getParams().getUserId();
		Long deptId = reqVo.getParams().getDeptId();
		
		PartnerInfo pi = loginUtils.getUser(userId);
        if (pi == null) {
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("未找到用户信息");
            return res;
        }
        
		int pageNo = reqVo.getParams().getPageNo();
		int pageSize = reqVo.getParams().getPageSize();
		PartnerInfoDto partnerInfoDto = new PartnerInfoDto();
		partnerInfoDto.setHigherid(pi.getHigherid());
		String checkStatus = reqVo.getParams().getCheckStatus();
		// 部门参数暂时未写入
		if(StringUtil.isNotNull(checkStatus)) {
			partnerInfoDto.setCheckStatusList(Arrays.asList(checkStatus));
		}else {
			// 如果没传，则人员查询当前公司的需要审核的注册人员
			partnerInfoDto.setCheckStatusList(Arrays.asList(CheckStatusEnum.TODO.getNo(), 
					CheckStatusEnum.FAILURE.getNo(), CheckStatusEnum.SUCCESS.getNo()));
		}
		if(deptId != null && deptId != 0) {
			Department department = departmentDao.selectById(deptId);
			if(department == null) {
				res.setResData(partnerCheckPageResDto);
				res.setResult(ResultEnum.SUCCESS.getNo());
				return res;
			}
			partnerInfoDto.setPermissionCode(department.getPermissionCode() + "%");
		}
		String partnerParam = reqVo.getParams().getPartnerInfo();
		if(StringUtil.isNotNull(partnerParam)) {
			partnerInfoDto.setSql("(t.account_name like '%" + partnerParam
					+ "%' or t.phone like '%" + partnerParam + "%')");
		}
		// 查询当前人员所有的审核列表
		// 只与审核状态有关，与删除标识无关
		List<PartnerInfo> partnerInfoList = partnerInfoService.searchCheckListByDto(partnerInfoDto, 
				(pageNo - 1) * pageSize, pageSize);
		int total = partnerInfoService.countSearchCheckListByDto(partnerInfoDto);
		if(!CollectionUtils.isEmpty(partnerInfoList)) {
			List<PartnerCheckResDto> partnerCheckResDtoList = new ArrayList<>(partnerInfoList.size());
			Page<PartnerCheckResDto> partnerCheckResDtoPage = new Page<>(total, partnerCheckResDtoList);
			partnerCheckResDtoPage.setTotalPages(total%pageSize == 0? (total/pageSize): (total/pageSize + 1));
//			partnerCheckResDtoPage.setRows(rows);
			partnerCheckPageResDto.setPartnerCheckResDtoPage(partnerCheckResDtoPage);
			List<Long> idList = new ArrayList<>();
			ModelInfo modelInfo = new ModelInfo();
			modelInfo.setRelIdList(idList);
			modelInfo.setModelType(ModelTypeEnum2.PARTNER_CHECK.getNo());
			partnerInfoList.forEach(partnerInfo -> {
				idList.add(partnerInfo.getId());
				
			});
			List<ModelInfo> modelInfoList = modelInfoService.selectListByDto(modelInfo);
			partnerInfoList.forEach(partnerInfo -> {
				PartnerCheckResDto partnerCheckResDto = new PartnerCheckResDto();
				partnerCheckResDto.setCheckUserId(partnerInfo.getId());
				partnerCheckResDto.setDeptName(partnerInfo.getCompanyName() + "-" + partnerInfo.getDepartmentName());
				partnerCheckResDto.setUserName(partnerInfo.getAccountName());
				if(partnerInfo.getCreateTime() != null) {
					String createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(partnerInfo.getCreateTime());
					partnerCheckResDto.setRegesiterTime(createTime);
				}
				partnerCheckResDto.setCheckStatus(partnerInfo.getCheckStatus());
				partnerCheckResDto.setCheckUserName(partnerInfo.getCheckUserName());
				partnerCheckResDto.setPhone(partnerInfo.getPhone());
				if(partnerInfo.getCheckTime() != null) {
					String checkTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(partnerInfo.getCheckTime());
					partnerCheckResDto.setCheckTime(checkTime);
				}
				partnerCheckResDto.setRemark(partnerInfo.getCheckRemark());
				if(!CollectionUtils.isEmpty(modelInfoList)) {
					// 把模板信息放入到对应的ID审核记录中
					modelInfoList.forEach(eachModelInfo -> {
						partnerCheckResDto.addModelInfoResDto(eachModelInfo);
					});
				}
				partnerCheckResDtoList.add(partnerCheckResDto);
			});
			
			
		}
		
		res.setResData(partnerCheckPageResDto);
		res.setResult(ResultEnum.SUCCESS.getNo());
		return res;
	}

}
